<!DOCTYPE html>
<html>
<head>
<title>Fig. 7.1 Physics Diagram</title>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
  }
  canvas {
    border: 1px solid #ccc;
    background-color: white;
  }
</style>
</head>
<body>
<canvas id="physicsCanvas" width="800" height="500"></canvas>
<script>
  const canvas = document.getElementById('physicsCanvas');
  const ctx = canvas.getContext('2d');

  // Define drawing parameters
  const cx = 200; // Center x of the sphere
  const cy = 200; // Center y of the sphere
  const R_pixels = 80; // Radius of the sphere in pixels
  const x_dist_pixels = 350; // Distance x in pixels
  const px = cx + x_dist_pixels; // x-coordinate of point P
  const py = cy; // y-coordinate of point P
  const y_for_x_line = cy + 120; // y-level for the distance 'x' measurement line
  const lineWidth = 2;
  const headlen = 10; // Length of arrowhead

  // Set default drawing styles
  ctx.lineWidth = lineWidth;
  ctx.strokeStyle = 'black';
  ctx.fillStyle = 'black';

  // --- 1. Draw the Sphere (Circle) ---
  ctx.beginPath();
  ctx.arc(cx, cy, R_pixels, 0, 2 * Math.PI);
  ctx.stroke();

  // --- 2. Draw the dashed horizontal axis line ---
  ctx.beginPath();
  ctx.setLineDash([10, 8]);
  ctx.moveTo(cx - R_pixels - 40, cy);
  ctx.lineTo(px + 80, cy);
  ctx.stroke();
  ctx.setLineDash([]); // Reset to solid line

  // --- 3. Draw the radius R ---
  // Calculate the point on the circumference for the radius line
  const angleR = (3 * Math.PI) / 4; // 135 degrees from positive x-axis
  const r_start_x = cx + R_pixels * Math.cos(angleR);
  const r_start_y = cy - R_pixels * Math.sin(angleR); // In canvas, y increases downwards

  // Draw the line for the radius (from circumference to center)
  ctx.beginPath();
  ctx.moveTo(r_start_x, r_start_y);
  ctx.lineTo(cx, cy);
  ctx.stroke();

  // Draw the arrowhead for R at the center
  const angleArrowR = Math.atan2(cy - r_start_y, cx - r_start_x);
  ctx.beginPath();
  ctx.moveTo(cx, cy);
  ctx.lineTo(cx - headlen * Math.cos(angleArrowR - Math.PI / 6), cy - headlen * Math.sin(angleArrowR - Math.PI / 6));
  ctx.moveTo(cx, cy);
  ctx.lineTo(cx - headlen * Math.cos(angleArrowR + Math.PI / 6), cy - headlen * Math.sin(angleArrowR + Math.PI / 6));
  ctx.stroke();

  // Draw the label 'R'
  ctx.font = 'italic 24px Arial';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillText('R', r_start_x - 20, r_start_y - 20);

  // --- 4. Draw point P ---
  // Draw the filled circle for point P
  ctx.beginPath();
  ctx.arc(px, py, 4, 0, 2 * Math.PI);
  ctx.fill();
  
  // Draw the label 'P'
  ctx.font = 'bold 24px Arial';
  ctx.fillText('P', px, py - 20);

  // --- 5. Draw dashed vertical lines for distance x measurement ---
  ctx.beginPath();
  ctx.setLineDash([5, 5]);
  // Line from sphere center down
  ctx.moveTo(cx, cy);
  ctx.lineTo(cx, y_for_x_line);
  // Line from point P down
  ctx.moveTo(px, py);
  ctx.lineTo(px, y_for_x_line);
  ctx.stroke();
  ctx.setLineDash([]); // Reset to solid line

  // --- 6. Draw the distance measurement line 'x' ---
  // The horizontal line
  ctx.beginPath();
  ctx.moveTo(cx, y_for_x_line);
  ctx.lineTo(px, y_for_x_line);
  ctx.stroke();
  
  // The left vertical tick
  ctx.beginPath();
  ctx.moveTo(cx, y_for_x_line - 8);
  ctx.lineTo(cx, y_for_x_line + 8);
  ctx.stroke();
  
  // The right arrowhead
  ctx.beginPath();
  ctx.moveTo(px, y_for_x_line);
  ctx.lineTo(px - headlen, y_for_x_line - headlen / 2);
  ctx.moveTo(px, y_for_x_line);
  ctx.lineTo(px - headlen, y_for_x_line + headlen / 2);
  ctx.stroke();
  
  // The label 'x'
  ctx.font = 'italic 24px Arial';
  ctx.fillText('x', (cx + px) / 2, y_for_x_line + 25);

  // --- 7. Draw the Figure label ---
  ctx.font = '20px Arial';
  ctx.fillText('Fig. 7.1', (cx + px) / 2, y_for_x_line + 80);

</script>
</body>
</html>